<?php

class Core_Plugin_Helper_Parser
{
    /**
     * $ele = array('id'=>int,
     * 'action_id'=>'string','params'=>string,'children'=>array())
     * @param array $ele
     * @param string $actionId
     * @return string
     */
    public function renderElement($element = null)
    {
        $params = array();
        $type = $element['type'];
        $spec = $element['spec'];

        if ('list' == $type)
        {
            /**
             * check spec to render.
             */
        }
        else
        if ('block' == $type)
        {
            $actionId = $element['name'];

            $params = array();

            $request = new Request_Simple($params, $actionId);

            $html = $request -> dispatch();

            unset($request);

            return sprintf("<div>%s</div>", $html);
        }

    }

    /**
     * $eles = array[array('id'=>int,
     * 'action_id'=>'string','params'=>string,'children'=>array())]
     * @param array $blocks
     *
     */
    public function renderElements($elements)
    {
        $xhtml = array();
        if (is_array($elements))
        {
            foreach ($elements as $element)
            {

                $xhtml[] = $this -> renderElement($element, null);

            }

        }
        return implode(PHP_EOL, $xhtml);
    }

    /**
     * check if accept empty content.
     */
    public function renderArea($allAreas, $areaName, $acceptEmpty = false)
    {
        $html = '';
        $area = isset($allAreas[$areaName]) ? $allAreas[$areaName] : array();

        if (!empty($area['children']))
        {
            $html = $this -> renderElements($area['children']);
        }

        if ($acceptEmpty == false && '' == $html)
        {
            return;
        }

        return sprintf('<div class="layout-area-%s">%s</div>', $areaName, $html);
    }

    /**
     * @param string $region
     * @param array $areas
     * @return string
     */
    public function renderLayout($section, $actionId = null, $siteId = null)
    {

       $table = Nuo::getTable('core@layout');
       
        /**
         * load section layout
         */
        list($layout, $areas) = $table -> loadLayout($section, $actionId, $siteId);
        
        if(null == $layout){
            return '';
        }
        
        $script = 'layout/areas/' . $layout['name'] . '.tpl';

        return View::render(array(
            'parser' => $this,
            'section' => $section,
            'areas' => $areas,
            'actionId' => $actionId,
            'siteId' => $siteId,
        ), $script);
    }

    /**
     * process render layout
     * @param string $page
     * @param string $actionId
     * @param int $siteId
     * @return string
     */
    protected function _renderPage($page = null, $actionId = null, $siteId = null)
    {
        if (null == $siteId)
        {
            $siteId = Nuo::getSiteId();
        }

        if (null == $actionId)
        {
            $actionId = Nuo::getRequest() -> getActionId();
        }

        if (null == $page)
        {
            $page = 'default';
        }

        $script = 'layout/pages/' . $page . '.tpl';
        
        return View::render(array(
            'page' => $page,
            'actionId' => $actionId,
            'siteId' => $siteId,
            'parser' => $this
        ), $script);
    }

    public function renderPage($page, $actionId = null, $siteId = null)
    {

        Nuo::fireEvent('onRenderPageStartup');

        $content = $this -> _renderPage($page, $actionId);

        Nuo::fireEvent('onRenderPageShutdown');

        $head = Nuo::getHelper('core@head');

        return '<html><head>' . $head . '</head><body>' . $content . '</body>' . '</html>';
    }

}
